package com.tgd.RingtoneContactManager;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.tgd.RingtoneContactManager.RingtoneAdapter.ViewHolder;

public class RingtoneFragment extends ListFragment {

	private Ringtone _selectedRingtone;
	private SimpleActivity _activity;
	private DAO _dao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		_activity = (SimpleActivity) getActivity();

		_dao = DAO.getInstance(_activity);

		setHasOptionsMenu(true);

		RingtoneAdapter ea = new RingtoneAdapter(_activity, _dao.getRingtones(
				Ringtone.Filter.ALL, Ringtone.Sort.CONTACT_COUNT));
		setListAdapter(ea);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.ringtone_actions, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		RingtoneAdapter ea;
		switch (item.getItemId()) {
		case R.id.filterAll:
			ea = new RingtoneAdapter(_activity,
					_dao.getRingtones(Ringtone.Filter.ALL));
			setListAdapter(ea);
			break;
		case R.id.filterWithContacts:
			ea = new RingtoneAdapter(_activity,
					_dao.getRingtones(Ringtone.Filter.WITH_CONTACT));
			setListAdapter(ea);
			break;
		case R.id.filterWithoutContacts:
			ea = new RingtoneAdapter(_activity,
					_dao.getRingtones(Ringtone.Filter.WITHOUT_CONTACT));
			setListAdapter(ea);
			break;
		case R.id.sortAlpha:
			ea = new RingtoneAdapter(_activity,
					_dao.getRingtones(Ringtone.Sort.NAME));
			setListAdapter(ea);
			break;
		case R.id.sortNumeric:
			ea = new RingtoneAdapter(_activity,
					_dao.getRingtones(Ringtone.Sort.CONTACT_COUNT));
			setListAdapter(ea);
			break;
		default:
			break;
		}

		getListView().invalidateViews();
		item.setChecked(true);
		return true;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		_selectedRingtone = ((ViewHolder) v.getTag()).rt;
		if (_actionMode == null) {
			_actionMode = _activity.startActionMode(mActionModeCallback);
		}
		_actionMode.setTitle(_selectedRingtone.getName());

		_actionMode.getMenu().findItem(R.id.setDefault)
				.setVisible(!_selectedRingtone.isDefault());
	}

	private ActionMode _actionMode;
	private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

		// Called when the action mode is created; startActionMode() was called
		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			// Inflate a menu resource providing context menu items
			MenuInflater inflater = mode.getMenuInflater();
			inflater.inflate(R.menu.ringtone_context, menu);

			return true;
		}

		// Called each time the action mode is shown. Always called after
		// onCreateActionMode, but
		// may be called multiple times if the mode is invalidated.
		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return false; // Return false if nothing is done
		}

		// Called when the user selects a contextual menu item
		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			switch (item.getItemId()) {
			case R.id.play:
				_dao.play(_selectedRingtone);
				break;
			case R.id.stop:
				_dao.stop();
				break;
			case R.id.addContact:
				AlertDialog contactAlert;
				final Contact[] contacts = _dao.getContacts(_selectedRingtone);
				boolean[] checkedItems = new boolean[contacts.length];
				String[] items = new String[contacts.length];
				int i = -1;

				for (Contact contact : contacts) {
					items[++i] = contact.getName();
					checkedItems[i] = contact.getRingtoneId().compareTo(
							_selectedRingtone.getId()) == 0;
				}

				Builder builder = new Builder(_activity);
				builder.setTitle("Pick a contact").setMultiChoiceItems(items,
						checkedItems, new OnMultiChoiceClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which, boolean isChecked) {
								// TODO Auto-generated method stub
								if (isChecked) {
									_dao.set(_selectedRingtone, contacts[which]);
								} else {
									_dao.setDefault(contacts[which]);
								}
								getListView().invalidateViews();
							}
						});
				contactAlert = builder.create();

				contactAlert.show();
				break;
			case R.id.addGroup:
				// TODO wire this up once groups are ready
				break;
			case R.id.setDefault:
				_dao.setDefault(_selectedRingtone);
				getListView().invalidateViews();
				_actionMode.finish();
				break;
			default:
				return false;
			}
			return true;
		}

		// Called when the user exits the action mode
		@Override
		public void onDestroyActionMode(ActionMode mode) {
			RingtoneAdapter ea = new RingtoneAdapter(_activity,
					_dao.getRingtones());
			setListAdapter(ea);
			getListView().clearChoices();
			getListView().invalidateViews();
			_actionMode = null;
		}
	};

}
